package blbetter.visual;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import blbetter.db.BLDataBase;
import blbetter.db.Tabelle;
import blbetter.db.TabelleEintrag;

public class BLTabellePanel extends JPanel {

	private static final long serialVersionUID = 1986304175871361729L;

	private String[] columnNames = new String[] { "", "Mannschaft", "Sp.", "S",
			"U", "N", "Tore", "Dif", "Pk." };

	public JTable tabelleTable = new JTable();

	public BLDataBase db;

	public BLTabellePanel(BLDataBase db) {
		this.db = db;
		// setData(2, 2);
		this.setLayout(new GridBagLayout());
		this.add(new JLabel("Tabelle"), new GridBagConstraints(0, 0, 1, 1, 0,
				0, GridBagConstraints.NORTH, GridBagConstraints.BOTH,
				new Insets(0, 0, 0, 0), 0, 0));
		this.add(tabelleTable.getTableHeader(), new GridBagConstraints(0, 1, 1,
				1, 0, 0, GridBagConstraints.NORTH, GridBagConstraints.BOTH,
				new Insets(0, 0, 0, 0), 0, 0));
		this.add(tabelleTable, new GridBagConstraints(0, 2, 1, 1, 0, 0,
				GridBagConstraints.NORTH, GridBagConstraints.BOTH, new Insets(
						0, 0, 0, 0), 0, 0));
		this.setVisible(true);
		// this.setPreferredSize(new Dimension(800, 500));
	}

	public void setData(int saisonId, int spieltag) {
		// System.out.println("BLTabellePanel.setData() : " + saisonId + " - "
		// + spieltag);
		Tabelle tabelle = new Tabelle(saisonId, spieltag, db);

		String[][] tableData = new String[18][columnNames.length];
		for (int i = 0; i < 18; i++) {
			TabelleEintrag eintrag = tabelle.eintraege.get(i);
			tableData[i][0] = "" + (i + 1);
			tableData[i][1] = db.getTeamName(eintrag.getTeamId());
			tableData[i][2] = "" + eintrag.spiele;
			tableData[i][3] = "" + eintrag.siege;
			tableData[i][4] = "" + eintrag.unentschieden;
			tableData[i][5] = "" + eintrag.niederlagen;
			tableData[i][6] = eintrag.geschossen + ":" + eintrag.gekriegt;
			tableData[i][7] = "" + eintrag.getTorDiff();
			tableData[i][8] = "" + eintrag.getPunkte();
		}

		tabelleTable.setModel(new DefaultTableModel(tableData, columnNames));

		tabelleTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
		tabelleTable.getColumn("Mannschaft").setPreferredWidth(200);
		tabelleTable.getColumnModel().getColumn(0).setPreferredWidth(30);
		tabelleTable.getColumn("Sp.").setPreferredWidth(30);
		tabelleTable.getColumn("S").setPreferredWidth(30);
		tabelleTable.getColumn("U").setPreferredWidth(30);
		tabelleTable.getColumn("N").setPreferredWidth(30);
		tabelleTable.getColumn("Tore").setPreferredWidth(45);
		tabelleTable.getColumn("Dif").setPreferredWidth(40);
		tabelleTable.getColumn("Pk.").setPreferredWidth(40);

		tabelleTable.setBorder(BorderFactory.createLineBorder(Color.black));
		tabelleTable.getTableHeader().setBorder(
				BorderFactory.createLineBorder(Color.black));
	}
}
